package com.example.administrator.myapplication;

import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.myapplication.interf.BackHandleInterface;
import com.example.administrator.myapplication.model.User;

public class MainActivity extends MyBaseActivity implements  BackHandleInterface {

    private TextView mTextMessage;
    private FragmentManager fm;
    private Fragment fragment;
    private Fragment mFragment;
    private final static String TAG = "MainActivity";

    {
       if (fm == null) {
           fm = getSupportFragmentManager();
       }
        fragment = fm.findFragmentById(R.id.content);
    }

    public FragmentManager getFm(){
        return this.fm;
    }

    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.wechat:
                    Log.d(TAG,"底部导航栏 "+getResources().getString(R.string.title_weChat)+" 按钮被触发！");
                    //Toast.makeText(MainActivity.this,getResources().getString(R.string.title_weChat),Toast.LENGTH_SHORT).show();
                    showToastMake(getResources().getString(R.string.title_weChat)).show();
                    fragment = new HomeFragment();
                    fm.beginTransaction().replace(R.id.content,fragment).commit();
                    return true;

                case R.id.contacts:
                    Log.d(TAG,"底部导航栏 "+getString(R.string.title_contacts)+" 按钮被触发！");

                    showToastMake(getResources().getString(R.string.title_contacts)).show();
                        fragment = new ContactsFragment();
                        fm.beginTransaction().replace(R.id.content,fragment).commit();
                    return true;

                case R.id.descover:
                    showToastMake(getResources().getString(R.string.title_discover)).show();
                    fragment = new DiscoverFragment();
                    fm.beginTransaction().replace(R.id.content, fragment).commit();
                    return true;
                case R.id.me:
                    Log.d(TAG,"底部导航栏 我 按钮被触发！");
                    showToastMake(getResources().getString(R.string.title_me)).show();
                    //第一步，取出传入当前Activity的extra信息
                      User user = ((User) getIntent().getBundleExtra("account").getSerializable("userObject"));
                    Log.i(TAG, "onNavigationItemSelected: 传入Activity的参数为："+user.getUsername());
                    //第二步，将对象需要传入的数据绑定到fragment中的argument中
                    fragment = MeFragment.newInstance(user);
                    fm.beginTransaction().replace(R.id.content, fragment).commit();

                    return true;
            }
            return false;
        }

    };



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

       // mTextMessage = (TextView) findViewById(R.id.message);
        BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);

        //在Activity级别设置后退图标按钮，主要用于一个Activity向另一个Activity后退
        //getSupportActionBar().setDisplayHomeAsUpEnabled(true);


        fragment = fm.findFragmentById(R.id.content);
        if(fragment == null){
            fragment = new HomeFragment();
            fm.beginTransaction().add(R.id.content,fragment).commit();
        }

    }

    @Override
    protected void onStart() {
        super.onStart();

    }

    @Override
    protected void onPause() {
        super.onPause();
        //在此处持久化数据是最安全的。
    }

    /**
     * 这个方法在某些时候将会在onPause()回调方法之后才执行
     * @param outState
     * @param outPersistentState
     */
    @Override
    public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
        super.onSaveInstanceState(outState, outPersistentState);
    }

    /**
     * 指定的选项被点击触发，比如上面的后退图标的点击事件。
     * 此处飞方法将优先于Fragment中的方法被执行。
     * @param item
     * @return
     */
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        /*switch (item.getItemId()){
            case android.R.id.home:
                Log.i(TAG,"后退图标事件触发！");
                return true;
            default:
                break;
        }*/
        return super.onOptionsItemSelected(item);
    }

    /**
     * 专用于手机上的back实体按钮
     * 当触摸智能手机上的back键时将自动触发
     */
    @Override
    public void onBackPressed() {
        Log.i(TAG,"智能手机的BACK键被触发！");
        if (mFragment instanceof SettingsFragment){
            ((SettingsFragment) mFragment).back();
        }else if (mFragment instanceof GloabFragment){
            ((GloabFragment) mFragment).back();
        }
        super.onBackPressed();

    }

    /**
     * 与上面的OnBackPressed类似。
     * 直接获取手机按键按下事件，兼容性更好，使用范围更广。
     * 常用的比如各种输入法系统。
     * 优先于onBackPressed()执行
     * @param keyCode
     * @param event
     * @return
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK){
            Log.i(TAG,"KeyDown被触发！");
        }
        return super.onKeyDown(keyCode, event);
    }


    @Override
    public void setSelectedFragment(Fragment fragment) {
        this.mFragment = fragment;
    }
}
